home *** CD-ROM | disk | FTP | other *** search
/ Total Java Scripts / Total Java Scripts.iso / JavaApplets / button / VROVER / vrover3.java < prev    next >
Encoding:
Java Source  |  1998-10-31  |  13.0 KB  |  374 lines

  1. /*
  2. ** vrover applet **
  3. (c) 1997 Vitas Ramanchauskas, vitas@webdon.com, http://webdon.com, ICQ:3024702
  4.  
  5. You may use this applet freely.
  6. All I ask is that you add a link to my home page: http://webdon.com,
  7. if possible.
  8. html tag: <a href="http://webdon.com">Vitas' site</a>
  9. ...or take my funny banner ;-)
  10.  
  11. About me.
  12. > 28 y.o. software developer.
  13.  
  14. > Applications, DLLs, Drivers (including VxDs), ActiveX controls,
  15.   utilities, libraries.. ..for Windows 95/NT, Win 3.x, DOS
  16.  
  17. > C++, Java, Forth, Assembler (including Pentium features,
  18.   virtual & protected modes, MMX.. ..as well as antique PDP-11 & i8080)
  19.  
  20. > Win32 SDK, DDK, Video for Windows SDK, OLE, ActiveX (including controls),
  21.   COM, MFC, ATL, Image processing, Multimedia, Internet programming (HTML,
  22.   CGI,ISAPI,Java,...), security & cryptography ...more!
  23.  
  24. > Deep knowledge of system internals
  25.  
  26. > Skilled in mathematics, physics & digital electronics
  27. */
  28.  
  29. import java.applet.*;
  30. import java.awt.*;
  31. import java.net.*;
  32.  
  33.  
  34. public class vrover3 extends Applet implements Runnable
  35. {
  36.         private int butCnt = 0;                                                         // buttons number
  37.         private boolean horiStyle = false;              // true if horizontal layout
  38.         private int activeIndex = -1;                                   // current button index
  39.         private Thread   m_vrover = null;
  40.  
  41.         private Graphics m_Graphics;
  42.         private Image    m_Images[];
  43.         private int      m_nImgWidth  = 0;
  44.         private int      m_nImgHeight = 0;
  45.         private boolean  m_fAllLoaded = false;
  46.         private final int NUM_IMAGES = 4;
  47.  
  48.         private String m_url[] = new String[50];
  49.         private String m_text[] = new String[50];
  50.         private String m_frame[] = new String[50];
  51.         private Color m_fgColor = new Color(255,255,40);
  52.         private Color m_bkColor = new Color(135,170,180);
  53.         private int w = 80, h = 32;
  54.  
  55.         private final String PARAM_url = "url";
  56.         private final String PARAM_frame = "frame";
  57.         private final String PARAM_text = "text";
  58.         private final String PARAM_bkColor = "bkColor";
  59.         private final String PARAM_fgColor = "fgColor";
  60.         private final String PARAM_w = "w";
  61.         private final String PARAM_h = "h";
  62.         private final String PARAM_style = "style";
  63.  
  64.         public vrover3()
  65.         {
  66.                 m_url[0] = "index.htm";
  67.                 m_text[0] = "Pressme";
  68.                 m_frame[0] = "_self";
  69.         }
  70.  
  71.         public String getAppletInfo()
  72.         {
  73.                 return "Name: vrover\r\n" +
  74.                        "Author: Vitas Ramanchauskas\r\n" +
  75.                        "Created solely";
  76.         }
  77.  
  78.         public String[][] getParameterInfo()
  79.         {
  80.                 String[][] info =
  81.                 {
  82.                         { PARAM_url, "String", "HREF" },
  83.                         { PARAM_frame, "String", "Target frame" },
  84.                         { PARAM_w, "Integer", "button width" },
  85.                         { PARAM_h, "Integer", "button height" },
  86.                         { PARAM_bkColor, "Integer", "Background Color" },
  87.                         { PARAM_fgColor, "Integer", "Foreground Color" },
  88.                         { PARAM_text, "String", "Text to be displayed" },
  89.                         { PARAM_style, "Integer", "Style" },
  90.                 };
  91.                 return info;
  92.         }
  93.  
  94.         private int parse(String dst[], String src)
  95.         {
  96.                 int x=0, i=0;
  97.                 while(true)
  98.                 {
  99.                         int n = src.indexOf(',', x);
  100.                         if(n==-1)
  101.                         {
  102.                                 dst[i] = src.substring(x);
  103.                                 break;
  104.                         }
  105.                         else
  106.                                 dst[i] = src.substring(x, n);
  107.                         dst[i] = dst[i].trim();
  108.                         x = n+1;
  109.                         i++;
  110.                 }
  111.                 return i+1;
  112.         }
  113.  
  114.         public void init()
  115.         {
  116.                 String param;
  117.  
  118.                 param = getParameter(PARAM_url);
  119.                 if (param != null)
  120.                         parse(m_url, param);
  121.  
  122.                 param = getParameter(PARAM_bkColor);
  123.                 if (param != null)
  124.                 {
  125.                         m_bkColor = new Color(Integer.parseInt(param));
  126.                         setBackground(m_bkColor);
  127.                 }
  128.  
  129.                 param = getParameter(PARAM_fgColor);
  130.                 if (param != null)
  131.                         m_fgColor = new Color(Integer.parseInt(param));
  132.  
  133.                 param = getParameter(PARAM_text);
  134.                 if (param != null)
  135.                         butCnt = parse(m_text, param);
  136.  
  137.                 param = getParameter(PARAM_frame);
  138.                 if (param != null)
  139.                         parse(m_frame, param);
  140.  
  141.                 param = getParameter(PARAM_w);
  142.                 if (param != null)
  143.                         w = Integer.parseInt(param);
  144.                 param = getParameter(PARAM_h);
  145.                 if (param != null)
  146.                         h = Integer.parseInt(param);
  147.                 param = getParameter(PARAM_style);
  148.                 if (param != null)
  149.                         horiStyle = (Integer.parseInt(param)==1);
  150.         }
  151.  
  152.         private void displayImage(Graphics g, int n, int phase)
  153.         {
  154.                 if (!m_fAllLoaded)
  155.                         return;
  156.  
  157.                 if(horiStyle)
  158.                         g.drawImage(m_Images[phase],
  159.                                            w*n+5, (h - m_nImgHeight) / 2, null);
  160.                 else
  161.                         g.drawImage(m_Images[phase],
  162.                                            5, n*h+(h - m_nImgHeight) / 2, null);
  163.         }
  164.  
  165.         private void draw1(Graphics g, int n)
  166.         {
  167.                 Rectangle r;
  168.                 if(horiStyle)
  169.                         r = new Rectangle(w*n,0,w,h);
  170.                 else
  171.                         r = new Rectangle(0,n*h,w,h);
  172.                 if (m_fAllLoaded)
  173.                 {
  174.                         g.clearRect(r.x, r.y, r.width, r.height);
  175.                         g.setColor(new Color(192,192,192));
  176.                         g.drawLine(r.x, r.y, r.x+r.width-2, r.y);
  177.                         g.drawLine(r.x, r.y, r.x, r.y+r.height-2);
  178.                         g.setColor(new Color(120,120,120));
  179.                         g.drawLine(r.x+r.width-2, r.y+1, r.x+r.width-2, r.y+r.height-2);
  180.                         g.drawLine(r.x+1, r.y+r.height-2, r.x+r.width-2, r.y+r.height-2);
  181.                         g.setColor(new Color(0,0,0));
  182.                         g.drawLine(r.x+r.width-1, r.y, r.x+r.width-1, r.y+r.height-1);
  183.                         g.drawLine(r.x, r.y+r.height-1, r.x+r.width-1, r.y+r.height-1);
  184.                         displayImage(g, n, 0);          // always in base (dark) state
  185.                 }
  186.                 g.setColor(m_fgColor);
  187.                 g.drawString(m_text[n], r.x+25, r.y+r.height/2 +
  188.                         g.getFontMetrics().getAscent()/2);
  189.         }
  190.  
  191.         public void paint(Graphics g)
  192.         {
  193.                 for(int i=0; i<butCnt; i++)
  194.                         draw1(g, i);
  195.         }
  196.  
  197.         public void start()
  198.         {
  199.                 setBackground(m_bkColor);
  200.                 if (m_vrover == null)
  201.                 {
  202.                         m_vrover = new Thread(this);
  203.                         m_vrover.start();
  204.                 }
  205.         }
  206.  
  207.         public void stop()
  208.         {
  209.                 if (m_vrover != null)
  210.                 {
  211.                         m_vrover.stop();
  212.                         m_vrover = null;
  213.                 }
  214.  
  215.         }
  216.  
  217.         public void run()
  218.         {
  219.     if (!m_fAllLoaded)
  220.                 {
  221.                         m_Graphics = getGraphics();
  222.                 repaint();
  223.                 m_Images   = new Image[NUM_IMAGES];
  224.  
  225.                 MediaTracker tracker = new MediaTracker(this);
  226.                 String strImage;
  227.  
  228.                 for (int i = 1; i <= NUM_IMAGES; i++)
  229.                 {
  230.                         strImage = "phase" + i + ".gif";
  231.                             m_Images[i-1] = getImage(getCodeBase(), strImage);
  232.           tracker.addImage(m_Images[i-1], 0);
  233.                 }
  234.                         try
  235.                         {
  236.                                 tracker.waitForAll();
  237.                                 m_fAllLoaded = !tracker.isErrorAny();
  238.                         }
  239.                         catch (InterruptedException e)
  240.                         {
  241.                         }
  242.  
  243.                         if (!m_fAllLoaded)
  244.                         {
  245.                             stop();
  246.                             m_Graphics.drawString("Error loading images!", 10, 40);
  247.                             return;
  248.                         }
  249.  
  250.                         // Assuming all images are same width and height.
  251.                     m_nImgWidth  = m_Images[0].getWidth(this);
  252.                     m_nImgHeight = m_Images[0].getHeight(this);
  253.         }
  254.                 repaint();
  255.  
  256.  
  257. // Yeah! You've found it! I want to know where my applet is used
  258. // Follow code (you'll remove it of course ;) ) tries to open
  259. // non-existent image at webdon.com...
  260. // ...and I'll see this request in my log...
  261. // It is possible to do applet copy-protection...
  262.                 MediaTracker trk = new MediaTracker(this);
  263.     try
  264.                 {trk.addImage(getImage(new URL("http://webdon.com/vrover3.gif")),0);
  265.                 try
  266.                 {       trk.waitForAll(4444);   }
  267.                 catch (InterruptedException e)
  268.                 {}
  269.                 }
  270.                 catch (MalformedURLException e)
  271.                 {}
  272.         }
  273.  
  274.         // converts (x,y) coordinate to Active Index (index of current button)
  275.         private int xy2ai(int x, int y)
  276.         {
  277.                 return horiStyle? x/w:y/h;
  278.         }
  279.  
  280.         private void validateAI(int x, int y)
  281.         {
  282.                 if(xy2ai(x,y) != activeIndex && m_fAllLoaded)
  283.                 {
  284.                         if(activeIndex!=-1)
  285.                         {
  286.                                 // turn off previous button indicator
  287.                                 displayImage(m_Graphics, activeIndex, 0);
  288.                         }
  289.                         // hilight new one
  290.                         activeIndex = xy2ai(x,y);
  291.                         displayImage(m_Graphics, activeIndex, 1);
  292.                 }
  293.         }
  294.  
  295.         public boolean mouseDown(Event evt, int x, int y)
  296.         {
  297.                 if(m_fAllLoaded)
  298.                 {
  299.                         // fade in
  300.                         validateAI(x, y);
  301.                         displayImage(m_Graphics, xy2ai(x,y), 2);
  302.                         try{
  303.                                 Thread.sleep(60);
  304.                         }
  305.                         catch (InterruptedException e)
  306.                         {}
  307.                         displayImage(m_Graphics, xy2ai(x,y), 3);
  308.                         try{
  309.                                 Thread.sleep(60);
  310.                         }
  311.                         catch (InterruptedException e)
  312.                         {}
  313.                 }
  314.                 return true;
  315.         }
  316.  
  317.         public boolean mouseUp(Event evt, int x, int y)
  318.         {
  319.                 if(activeIndex==-1)
  320.                         return true;
  321.                 // fade out
  322.                 validateAI(x, y);
  323.                 displayImage(m_Graphics, xy2ai(x,y), 2);
  324.                 try
  325.                 {
  326.                         Thread.sleep(60);
  327.                 }
  328.                 catch (InterruptedException e)
  329.                 {
  330.                         ;
  331.                 }
  332.                 displayImage(m_Graphics, xy2ai(x,y), 1);
  333.  
  334.                 // jump through hyperlink
  335.                 URL theUrl;
  336.                 try {
  337.                         theUrl = new URL(getDocumentBase(), m_url[activeIndex]);
  338.                         getAppletContext().showDocument(theUrl,
  339.                                 (m_frame[activeIndex]==null)? "_self":m_frame[activeIndex]);
  340.                 }
  341.                 catch (MalformedURLException e)
  342.                 {
  343.                 }
  344.  
  345.                 return true;
  346.         }
  347.  
  348.         public boolean mouseEnter(Event evt, int x, int y)
  349.         {
  350.                 if(m_fAllLoaded)
  351.                 {
  352.                         activeIndex = xy2ai(x,y);
  353.                         displayImage(m_Graphics, activeIndex, 1);
  354.                 }
  355.                 return true;
  356.         }
  357.  
  358.         public boolean mouseMove(Event evt, int x, int y)
  359.         {
  360.                 validateAI(x,y);
  361.                 return true;
  362.         }
  363.  
  364.         public boolean mouseExit(Event evt, int x, int y)
  365.         {
  366.                 if(m_fAllLoaded)
  367.                 {
  368.                         displayImage(m_Graphics, activeIndex, 0);
  369.                         activeIndex = -1;
  370.                 }
  371.                 return true;
  372.         }
  373. }
  374.